package com.windupurnomo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.windupurnomo.data.ActivityManagerData;
import com.windupurnomo.entity.Condition;
import com.windupurnomo.entity.ListTask;
import com.windupurnomo.entity.Player;
import com.windupurnomo.entity.Resource;
import com.windupurnomo.entity.ShareInfo;
import com.windupurnomo.entity.TaskDay;
import com.windupurnomo.util.Function;
import com.windupurnomo.util.MyQuery;
import com.windupurnomo.util.Util;

public class SelectedTask extends Activity{
	private ShareInfo shareInfo;
	private Player player;
	private ListTask listTaskDay = null;
	private ImageView imageTask = null;
	private TextView txtJam, txtStatus;
	private long id = 0;
	private List<TaskDay> ltd = new ArrayList<TaskDay>();
	private TaskDay taskDay = null;
	private int TIME = 0;
	
	public void onCreate(Bundle i){
		super.onCreate(i);
		setContentView(R.layout.selected_task);
		imageTask = (ImageView) findViewById(R.id.imageTask);
		txtJam = (TextView) findViewById(R.id.txtJam);
		txtStatus = (TextView) findViewById(R.id.status);
		
		shareInfo = (ShareInfo) getIntent().getSerializableExtra("shareInfo");
		id = (Long) getIntent().getSerializableExtra("selectedTask");
		listTaskDay = shareInfo.getListTask();
		TIME = shareInfo.getTIME();
		player = shareInfo.getPlayer();
		
		ltd = listTaskDay.getTaskDays();
		taskDay = ltd.get((int)id);
		
		txtJam.setText(Function.timeFormat(TIME)+"\n");
		imageTask.setImageResource(Function.getTaskPictName(taskDay.getTask().getId()));
	}	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.selected_task_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem){
		switch(menuItem.getItemId()){
			case R.id.done:{
				int nextTime = TIME + taskDay.getTask().getDuration();
				int start = taskDay.getStart();
				int finish = taskDay.getFinish();
				int rainStart = taskDay.getDay().getStart();
				int rainFinish = taskDay.getDay().getFinish();
				int weather = taskDay.getDay().getWeather();
				int taskPlace = taskDay.getTask().getPlace();
				boolean scs = Function.isTaskSuccess(TIME, start, finish, nextTime, weather, rainStart, rainFinish, taskPlace);
				if(scs) success();
				else failed();
				txtJam.setText(Function.timeFormat(TIME)+"\n");
				ltd.remove((int)id);
				listTaskDay.setTaskDays(ltd);
				TIME += taskDay.getTask().getDuration();
				shareInfo.setListTask(listTaskDay);
				shareInfo.setTIME(TIME);
				Intent i = null;
				shareInfo.setPlayerStatus(Util.Player.OLD_PLAYER);
				if(shareInfo.getNumFailed()>=3){
					i = new Intent(this, Home.class);
					ActivityManagerData acm = new ActivityManagerData(this);
			        SQLiteDatabase db = acm.getWritableDatabase();
					MyQuery.PlayerDAO.updatePlayer(shareInfo.getPlayer(), db);
				}
				else i = new Intent(this, Tasks.class);
				i.putExtra("shareInfo", shareInfo);
				startActivity(i);
				break;
			}
			case R.id.back: {
				Intent i = new Intent(this, Tasks.class);
				i.putExtra("shareInfo", shareInfo);
				startActivity(i);
				break;
			}
		}
		return true;
	}
	

	private void failed(){
		txtStatus.setText("FAILED\n");
		int n = shareInfo.getNumFailed();
		shareInfo.setNumFailed(n+1);
	}
	
	private void success(){
		txtStatus.setText("SUCCESS\n");
		//menjumlahkan resource dan condition player dengan resource dan condition yang ada pada task
		Resource playerResource, taskResource = null;
		Condition playerCondition, taskCondition = null;
		
		int water, food, paper;
		int power, social, finance, eiq;
		playerResource = player.getResource();
		taskResource = taskDay.getTask().getResource();
		playerCondition = player.getCondition();
		taskCondition = taskDay.getTask().getCondition();
		
		water = playerResource.getWater()+taskResource.getWater();
		food = playerResource.getFood()+taskResource.getFood();
		paper = playerResource.getPaper()+taskResource.getPaper();
		
		power = playerCondition.getPower()+taskCondition.getPower();
		social = playerCondition.getSocial()+taskCondition.getSocial();
		finance = playerCondition.getFinance()+taskCondition.getFinance();
		eiq = playerCondition.getEiq()+taskCondition.getEiq();
		
		playerResource.setWater(water);
		playerResource.setFood(food);
		playerResource.setPaper(paper);
		
		playerCondition.setPower(power);
		playerCondition.setSocial(social);
		playerCondition.setFinance(finance);
		playerCondition.setEiq(eiq);
		
		player.setResource(playerResource);
		player.setCondition(playerCondition);
		player.setScore(Function.score(shareInfo));
		
		shareInfo.setPlayer(player);
		shareInfo.setNumScs(shareInfo.getNumScs()+1);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	    	//Toast.makeText(this, "Menu", Toast.LENGTH_LONG).show();
	        return true;
	    }

	    return super.onKeyDown(keyCode, event);
	}
	
}
